package com.bookcycle.panda.webfront.controller;

import com.baomidou.kisso.SSOHelper;
import com.baomidou.kisso.security.token.SSOToken;
import com.bookcycle.panda.webfront.constant.URLConstant;
import com.bookcycle.panda.webfront.vo.UserInformation;
import dto.RegisterDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @author Mason
 * @date 2018/3/6
 */
@Controller
public class UserController extends BaseController{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "showUser",method = RequestMethod.POST)
    @ResponseBody
    public UserInformation showUser(Model model){
        SSOToken ssoToken=SSOHelper.getSSOToken(request);
        String userId=ssoToken.getId();
        RestTemplate restTemplate = new RestTemplate();
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("userId", userId);

        if(userId != null){
            ResponseEntity<UserInformation> responseEntity = restTemplate.postForEntity(URLConstant.URL_GET_USER_INFORMATION, param, UserInformation.class);
            UserInformation userInformation= responseEntity.getBody();
            return  userInformation;
        } else {
            return new UserInformation();
        }
    }

    @RequestMapping(value = "register",method = RequestMethod.POST)
    @ResponseBody
    public String Register(@RequestParam("account")String account,
                           @RequestParam("password")String password,
                           @RequestParam("passwordConfirm")String passwordConfirm){
        RestTemplate restTemplate=new RestTemplate();
        MultiValueMap<String,Object> param=new LinkedMultiValueMap<>();

        param.add("account","MasonWebTest6");
        param.add("password","MasonWebTest6");
        param.add("passwordConfirm","MasonWebTest6");
        ResponseEntity<RegisterDTO> responseEntity=restTemplate.postForEntity(URLConstant.URL_REGISTER_BY_ACCOUNT,param,RegisterDTO.class);
        return responseEntity.getBody().getStatus();
    }
}
